package com.zjf.bbs.service.impl.ask;

import java.util.Date;
import java.util.Iterator;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zjf.bbs.dao.AskDao;
import com.zjf.bbs.entity.Ask;
import com.zjf.bbs.entity.UserGroup;
import com.zjf.bbs.service.ask.AskCallBack;
import com.zjf.bbs.service.ask.AskService;
@Service(value="askService")
public class AskServiceImpl implements AskService {
	@Resource(name="askDao")
	private AskDao askDao;
	@Resource(name="userGroupCallBack")
	private AskCallBack callback;
	public void askCancel(Ask ask) {
		ask = askDao.load(Ask.class, ask.getId());
		ask.setIsEnd(true);
		ask.setIsSuccess(false);
		askDao.update(ask);

	}

	public void askPass(Ask ask) {
		ask = askDao.load(Ask.class, ask.getId());
		
		if(!ask.getIsEnd()){
			
			ask.setStep(ask.getStep()+1);
			UserGroup nextVerifyer = null;
			Iterator<UserGroup> it =  ask.getWorkFlow().getVerifyGroups().iterator();
			it.next();
			for(int i = 0;i<ask.getStep();i++){
				if(it.hasNext())
				nextVerifyer = it.next();
			}
			ask.setNextVerifyer(nextVerifyer);
			ask.setEndDate(new Date());
			askDao.update(ask);
		}
		if(ask.getStep() >= ask.getWorkFlow().getVerifyGroups().size()){
			
			ask.setIsEnd(true);
			ask.setIsSuccess(true);
			ask.setEndDate(new Date());
			askDao.update(ask);
			askinvoke(ask, callback);
		}
	}

	public void askinvoke(Ask ask,AskCallBack callback) {
		callback.invoke(ask);
	}
	
}
